package list

import (
	"github.com/therecipe/qt/widgets"
	"github.com/therecipe/qt/core"
	"github.com/therecipe/qt/gui"
)
type Delegate struct {
	widgets.QStyledItemDelegate //don't use *pointers or it won't work
}


func InitDelegate() *Delegate {
	item := NewDelegate(nil)
	item.ConnectPaint(item.paint)
	item.ConnectSizeHint(item.sizeHint)
	item.ConnectEditorEvent(item.editEvent)
	return item
}

func (this *Delegate) paint(painter *gui.QPainter, option *widgets.QStyleOptionViewItem, index *core.QModelIndex){
	if option.State() & widgets.QStyle__State_Selected != 0{
		painter.FillRect6(option.Rect(),gui.NewQColor6("lightsteelblue"))
	}
	height := option.Rect().Height()
	width := option.Rect().Width()
	x := option.Rect().X()
	y := option.Rect().Y()

	index_rect := core.NewQRect4(x,y,int(float32(width)*0.1),height)
	title_rect := core.NewQRect3(index_rect.TopRight(),core.NewQSize2(int(float32(width)*0.7),int(float32(height)*0.5)))
	sub_title_rect := core.NewQRect3(title_rect.BottomLeft(),core.NewQSize2(int(float32(width)*0.7),int(float32(height)*0.5)))
	edit_rect := core.NewQRect3(title_rect.TopRight(),core.NewQSize2(int(float32(width)*0.2),int(float32(height)*0.5-1)))
	widgets.QApplication_Style().DrawItemText(painter, index_rect,int(core.Qt__AlignCenter), widgets.QApplication_Palette(nil), true,
		string(index.Row()+1),gui.QPalette__WindowText)
	widgets.QApplication_Style().DrawItemText(painter, title_rect, int(core.Qt__AlignLeft|core.Qt__AlignVCenter),
		widgets.QApplication_Palette(nil), true, index.Data(SongData__TitleRole).ToString(),gui.QPalette__WindowText)
	widgets.QApplication_Style().DrawItemText(painter, sub_title_rect, int(core.Qt__AlignLeft|core.Qt__AlignVCenter),
		widgets.QApplication_Palette(nil), true, index.Data(SongData__ArtistRole).ToString()+"-"+index.Data(SongData__AlbumRole).ToString(),gui.QPalette__WindowText)
	widgets.QApplication_Style().DrawItemText(painter, edit_rect, int(core.Qt__AlignLeft|core.Qt__AlignVCenter), widgets.QApplication_Palette(nil), true,
		"...",gui.QPalette__WindowText)
	oldpen := painter.Pen()
	painter.SetPen(gui.NewQPen3(gui.NewQColor6("#96979c")))
	painter.DrawLine4(sub_title_rect.BottomLeft(),sub_title_rect.BottomRight())
	painter.SetPen(oldpen)
}

func (this *Delegate) sizeHint(option *widgets.QStyleOptionViewItem, index *core.QModelIndex) *core.QSize{
	size := widgets.NewQStyledItemDelegate(nil).SizeHint(option,index)
	size.SetHeight(40)
	return size
}

func (this Delegate) editEvent(event *core.QEvent, model *core.QAbstractItemModel, option *widgets.QStyleOptionViewItem, index *core.QModelIndex) bool{
	//height := option.Rect().Height()
	//width := option.Rect().Width()
	//x := option.Rect().X()
	//y := option.Rect().Y()
	//
	//index_rect := core.NewQRect4(x,y,width*0.1,height)
	//center_rect := core.NewQRect3(index_rect.TopRight(),core.NewQSize2(width*0.7,height))
	//edit_rect := core.NewQRect3(center_rect.TopRight(),core.NewQSize2(width*0.2,height*0.5))
	return widgets.NewQStyledItemDelegate(nil).EditorEvent(event,model,option,index)
}
